package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import www.woniuxy.com.class112.dao.PolicyDetailMapper;
import www.woniuxy.com.class112.dao.PolicyMapper;
import www.woniuxy.com.class112.dto.PolicyDto;
import www.woniuxy.com.class112.entity.Policy;
import www.woniuxy.com.class112.entity.PolicyDetail;
import www.woniuxy.com.class112.param.achievement.AchievementCompileParam;
import www.woniuxy.com.class112.param.achievement.AchievementFdAllParam;
import www.woniuxy.com.class112.param.achievement.AddAchievementParam;
import www.woniuxy.com.class112.service.AchievementService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AchievementServiceImpl implements AchievementService {
    @Resource
    private PolicyMapper policyMapper;
    @Resource
    private PolicyDetailMapper policyDetailMapper;
    @Override
    public PageInfo<PolicyDto> achievementfdAll(AchievementFdAllParam param) {
        PageHelper.startPage(param.getPageIndex() == null ? 1 : param.getPageIndex(), param.getPageSize() == null ? 3 : param.getPageSize());
        List<PolicyDto> policyDtos =policyMapper.achievementAll(param.getTitle(), param.getSenderId());

        return new PageInfo<>(policyDtos);
    }

    @Override
    public void delistachievement(Long policyId) {
        Policy policy = policyMapper.selectByPrimaryKey(policyId);

        if (policy == null){
            throw new RuntimeException("该成果转化不存在");
        }
        if (!policy.getSenderUnit().equals("成果转化")){
            throw new RuntimeException("不是成果转化类型");
        }
        if (policy.getStateId()!=0){
            throw new RuntimeException("该成果转化状态异常");
        }
        policy.setStateId(1L);
        policyMapper.updateByPrimaryKeySelective(policy);
    }

    @Override
    public void enableachievement(Long policyId) {
        Policy policy = policyMapper.selectByPrimaryKey(policyId);
        if (policy == null){
            throw new RuntimeException("该成果转化不存在");
        }
        if (policy.getStateId()!=1){
            throw new RuntimeException("该成果转化状态异常");
        }
        if (!policy.getSenderUnit().equals("成果转化")){
            throw new RuntimeException("不是成果转化类型");
        }
        policy.setStateId(0L);
        policyMapper.updateByPrimaryKeySelective(policy);
    }

    @Override
    public void deleteachievement(Long policyId) {
        Policy policy = policyMapper.selectByPrimaryKey(policyId);
        if (policy == null){
            throw new RuntimeException("该成果转化不存在");
        }
        if (policy.getStateId()==2){
            throw new RuntimeException("该成果转化状态异常");
        }
        if (!policy.getSenderUnit().equals("成果转化")){
            throw new RuntimeException("不是成果转化类型");
        }
        policy.setStateId(2L);
        policyMapper.updateByPrimaryKeySelective(policy);
    }

    @Override
    public void addachievement(AddAchievementParam param) {
        Policy policy = BeanUtil.toBean(param, Policy.class);
        System.out.println(policy);

        policy.setCreatedTime(new Date());
        policy.setSenderUnit("成果转化");
        policy.setStateId(0L);
        policy.setPageView(0L);
        policyMapper.insertSelective(policy);
        PolicyDetail policyDetail = new PolicyDetail();
        policyDetail.setPolicyId(policy.getPolicyId());
        policyDetail.setImageUrl(param.getImageUrl());
        policyDetail.setnoum(param.getText());
        policyDetailMapper.insertSelective(policyDetail);
        System.out.println(policy);
    }

    @Override
    public void compileachievement(AchievementCompileParam param) {
        {
            Policy policy = policyMapper.selectByPrimaryKey(param.getPolicyId());
            if (policy == null){
                throw new RuntimeException("该成果转化不存在");
            }
            if (!policy.getSenderUnit().equals("成果转化")){
                throw new RuntimeException("不是成果转化类型");
            }
            policy.setTitle(param.getTitle());
            policy.setSenderId(param.getSenderId());
            policy.setSourceId(param.getSourceId());

            PolicyDetail policyDetail = policyDetailMapper.selectByPolicyId(param.getPolicyId());
            if (policyDetail == null){
                throw new RuntimeException("该成果转化详情不存在");
            }
            policyMapper.updateByPrimaryKeySelective(policy);
            policyDetail.setImageUrl(param.getImageUrl());
            policyDetail.setnoum(param.getText());
            policyDetailMapper.updateByPrimaryKeySelective(policyDetail);
        }
//        @Override
//        public void tapNUm(Long id) {
//            Policy policy = policyMapper.selectByPrimaryKey(id);
//            if (policy == null){
//                throw new RuntimeException("该政策不存在");
//            }
//            if (!policy.getSenderUnit().equals("政策")){
//                throw new RuntimeException("不是政策类型");
//            }
//            policy.setPageView(policy.getPageView()+1);
//            policyMapper.updateByPrimaryKeySelective(policy);
//        }
    }

    @Override
    public void tapNUm(Long id) {
        Policy policy = policyMapper.selectByPrimaryKey(id);
        if (policy == null){
            throw new RuntimeException("该成果转化不存在");
        }
        if (!policy.getSenderUnit().equals("成果转化")){
            throw new RuntimeException("不是成果转化类型");
        }
        policy.setPageView(policy.getPageView()+1);
        policyMapper.updateByPrimaryKeySelective(policy);
    }

    @Override
    public PageInfo<PolicyDto> userachievementfdAll(AchievementFdAllParam param) {
        PageHelper.startPage(param.getPageIndex() == null ? 1 : param.getPageIndex(), param.getPageSize() == null ? 3 : param.getPageSize());
        List<PolicyDto> policyDtos =policyMapper.userachievementfdAll(param.getTitle(), param.getSenderId());

        return new PageInfo<>(policyDtos);
    }
}
